﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Model;
using Kaidou.Persister;
using Kaidou.Util;
using Kaidou.Bll;
using System.Collections.Generic;
using System.Globalization;

public partial class seal_seal_order_represen_detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];
        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
        }

        if (loginModel.isKumiaiin() && sessioncontrol.control())
        {
            Response.Redirect("../control.aspx");
        }

        if (!IsPostBack)
        {
            MST_UnionPersister dao1 = new MST_UnionPersister();
            MST_Union_BranchPersister dao2=new MST_Union_BranchPersister();
            TrnJuchuh juchu_h = (TrnJuchuh)Session["CHECKED_JUCHU_H"];
            DateTime? juchudate = juchu_h.JuchuDate;
            string rcv_date_y = ((DateTime)juchudate).ToString("yy");
            string rcv_date_m = ((DateTime)juchudate).Month.ToString();
            string rcv_date_d = ((DateTime)juchudate).Day.ToString();
            string ord_date_y = rcv_date_y;
            string ord_date_m = rcv_date_m;
            string ord_date_d = rcv_date_d;
            if ("10".Equals(juchu_h.JuchuClss))
            {
                RCV_P.Visible = false;
            }
            else
            {
                RCV_P.Visible = true;
            }
            SEAL_ORDER_REPRESEN_RCV_DATE_Y.Text = rcv_date_y;
            SEAL_ORDER_REPRESEN_RCV_DATE_M.Text = rcv_date_m;
            SEAL_ORDER_REPRESEN_RCV_DATE_D.Text = rcv_date_d;
            SEAL_ORDER_REPRESEN_RCV_TIME.Text = "0000";
            SEAL_ORDER_REPRESEN_ORDER_DATE_Y.Text = ord_date_y;
            SEAL_ORDER_REPRESEN_ORDER_DATE_M.Text = ord_date_m;
            SEAL_ORDER_REPRESEN_ORDER_DATE_D.Text = ord_date_d;
            unioncode.Text = juchu_h.UnionCode;
            MstUnion union = dao1.getDetail(juchu_h.UnionCode);
            unioncodename.Text = union.OfficeName;
            if (juchu_h.BranchCode == 0)
            {
                branchname.Text = "000 " + "本店";
            }
            else
            {
                branchname.Text = juchu_h.BranchCode + dao2.getDetail(juchu_h.UnionCode, juchu_h.BranchCode).officename;
            }
            if (juchu_h.BranchCode == 0)
            {
                TRN_JUCHU_TANTO_NAME.Text = union.TantoName;
            }
            else
            {
                TRN_JUCHU_TANTO_NAME.Text = dao2.getDetail(juchu_h.UnionCode, juchu_h.BranchCode).tantoname;
            }
            TRADER_REG_NO12.Text = StringUtil.isEmpty(union.TraderRegNo)?"": union.TraderRegNo.Substring(0,2);
            TRADER_REG_NO22.Text = StringUtil.isEmpty(union.TraderRegNo) ? "" : union.TraderRegNo.Substring(2, 5);
            if (!StringUtil.isEmpty(union.TraderRegNo))
            {
                TRADER_REG_NO_LABEL.Text = union.OfficeName == null ? "" : union.OfficeName;
            }

        }

    }

    protected void login_confirm(object sender, EventArgs e)
    {

        try
        {
            Order order = new Order();

            //モデルに値をセットする。(TRN_Juchu_H)
            TrnJuchuh tjh = (TrnJuchuh)Session["CHECKED_JUCHU_H"];
            tjh.JuchuTantoName = TRN_JUCHU_TANTO_NAME.Text;
            tjh.FaxRcvYear = SEAL_ORDER_REPRESEN_RCV_DATE_Y.Text;
            tjh.FaxRcvMonth = SEAL_ORDER_REPRESEN_RCV_DATE_M.Text;
            tjh.FaxRcvDay = SEAL_ORDER_REPRESEN_RCV_DATE_D.Text;
            tjh.FaxRcvTime = SEAL_ORDER_REPRESEN_RCV_TIME.Text;

            tjh.HopeYear = TRN_HOPE_YEAR.Text;
            tjh.HopeMonth = TRN_HOPE_MONTH.Text;
            tjh.HopeDay = TRN_HOPE_DAY.Text;

            tjh.JuchuYear = SEAL_ORDER_REPRESEN_ORDER_DATE_Y.Text;
            tjh.JuchuMonth = SEAL_ORDER_REPRESEN_ORDER_DATE_M.Text;
            tjh.JuchuDay = SEAL_ORDER_REPRESEN_ORDER_DATE_D.Text;

            CultureInfo culture = new CultureInfo("ja-JP");

            if (StringUtil.isValidDate(SEAL_ORDER_REPRESEN_RCV_DATE_Y.Text, SEAL_ORDER_REPRESEN_RCV_DATE_M.Text, SEAL_ORDER_REPRESEN_RCV_DATE_D.Text))
            {
                if (SEAL_ORDER_REPRESEN_RCV_TIME.Text.Length == 4)
                {
                    int h = int.Parse(tjh.FaxRcvTime.Substring(0, 2));
                    int m = int.Parse(tjh.FaxRcvTime.Substring(2, 2));
                    if (h < 0 || h > 23 || m < 0 || m > 59)
                    {
                    }
                    else
                    {
                        tjh.FaxRcvDate = Convert.ToDateTime((int.Parse(tjh.FaxRcvYear) + 2000) + "/" + int.Parse(tjh.FaxRcvMonth) + "/" + int.Parse(tjh.FaxRcvDay) + " " + tjh.FaxRcvTime.Substring(0, 2) + ":" + tjh.FaxRcvTime.Substring(2, 2), culture);
                    }
                }
                else if (SEAL_ORDER_REPRESEN_RCV_TIME.Text.Length == 0)
                {
                    tjh.FaxRcvDate = Convert.ToDateTime((int.Parse(tjh.FaxRcvYear) + 2000) + "/" + int.Parse(tjh.FaxRcvMonth) + "/" + int.Parse(tjh.FaxRcvDay), culture);
                }

            }
            else if (StringUtil.isEmpty(TRN_HOPE_YEAR.Text) && StringUtil.isEmpty(TRN_HOPE_MONTH.Text) && StringUtil.isEmpty(TRN_HOPE_DAY.Text))
            {
                tjh.HopeDate = null;
            }


                
            if (StringUtil.isValidDate(TRN_HOPE_YEAR.Text, TRN_HOPE_MONTH.Text, TRN_HOPE_DAY.Text))
            {

                tjh.HopeDate = Convert.ToDateTime((int.Parse(tjh.HopeYear) + 2000) + "/" + int.Parse(tjh.HopeMonth) + "/" + int.Parse(tjh.HopeDay), culture);

            }
            else if (StringUtil.isEmpty(TRN_HOPE_YEAR.Text) && StringUtil.isEmpty(TRN_HOPE_MONTH.Text) && StringUtil.isEmpty(TRN_HOPE_DAY.Text))
            {
                tjh.HopeDate = null;
            }

            if (StringUtil.isValidDate(SEAL_ORDER_REPRESEN_ORDER_DATE_Y.Text, SEAL_ORDER_REPRESEN_ORDER_DATE_M.Text, SEAL_ORDER_REPRESEN_ORDER_DATE_D.Text))
            {

                tjh.JuchuDate = Convert.ToDateTime((int.Parse(tjh.JuchuYear) + 2000) + "/" + int.Parse(tjh.JuchuMonth) + "/" + int.Parse(tjh.JuchuDay), culture);

            }
            else if (StringUtil.isEmpty(TRN_HOPE_YEAR.Text) && StringUtil.isEmpty(TRN_HOPE_MONTH.Text) && StringUtil.isEmpty(TRN_HOPE_DAY.Text))
            {
                tjh.HopeDate = null;
            }

            tjh.HopeTimeClss = TRN_HOPE_TIME_CLSS1.Checked ? TrnJuchuh.HOPE_TIME_CLSS_NONE : (TRN_HOPE_TIME_CLSS2.Checked ? TrnJuchuh.HOPE_TIME_CLSS_MORNING : TrnJuchuh.HOPE_TIME_CLSS_AFTERNOON);
            tjh.TotalSealQty = 0;
            //FAX OCRは使用しないので0固定
            tjh.OcrOrderQty = 0;

            TrnJuchud[] model = new TrnJuchud[5];
            TrnJuchud tmpmodel = null;
            TrnJuchudId id = null;
            string sq = null;

            //モデルに値をセットする。
            tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo = "";
            id.JuchuRecNo = 1;
            tmpmodel.Id = id;
            tmpmodel.PrintClss = TrnJuchud.PRINT_CLSS_NO;
            tmpmodel.TraderRegNo1 = TRADER_REG_NO11.Value;
            tmpmodel.TraderRegNo2 = TRADER_REG_NO21.Value;
            tmpmodel.TraderRegNo = TRADER_REG_NO11.Value + TRADER_REG_NO21.Value;
            sq = TRN_SEAL_QTY1.Text;
            if (StringUtil.isEmpty(sq))
            {
                tmpmodel.SealQty = -1;
            }
            else if (sq.IndexOf("-") != -1)
            {
                //マイナスが入力された場合には
                tmpmodel.SealQty = -2;
            }
            else if (!StringUtil.isHankakuNumber(sq))
            {
                tmpmodel.SealQty = -2;
            }
            else
            {
                tmpmodel.SealQty = int.Parse(TRN_SEAL_QTY1.Text);
            }
            if (tmpmodel.SealQty == -1)
            {
                model[0] = null;
                LogManager.info("seal_seal_order_represen_detail getJuchuDFromParameter null! : 1");
            }
            else
            {
                LogManager.info("seal_seal_order_represen_detail getJuchuDFromParameter set!  : 1");
                model[0] = tmpmodel;
            }

            //モデルに値をセットする。
            tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo = "";
            id.JuchuRecNo = 2;
            tmpmodel.Id = id;
            tmpmodel.PrintClss = TrnJuchud.PRINT_CLSS_NO;
            tmpmodel.TraderRegNo1 = TRADER_REG_NO12.Text;
            tmpmodel.TraderRegNo2 = TRADER_REG_NO22.Text;
            tmpmodel.TraderRegNo = TRADER_REG_NO12.Text + TRADER_REG_NO22.Text;
            sq = TRN_SEAL_QTY2.Text;
            if (StringUtil.isEmpty(sq))
            {
                tmpmodel.SealQty = -1;
            }
            else if (sq.IndexOf("-") != -1)
            {
                //マイナスが入力された場合には
                tmpmodel.SealQty = -2;
            }
            else if (!StringUtil.isHankakuNumber(sq))
            {
                tmpmodel.SealQty = -2;
            }
            else
            {
                tmpmodel.SealQty = int.Parse(TRN_SEAL_QTY2.Text);
            }
            if (tmpmodel.SealQty == -1)
            {
                model[1] = null;
                LogManager.info("seal_seal_order_represen_detail getJuchuDFromParameter null! : 2");
            }
            else
            {
                LogManager.info("seal_seal_order_represen_detail getJuchuDFromParameter set!  : 2");
                model[1] = tmpmodel;
            }

            //モデルに値をセットする。
            tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo = "";
            id.JuchuRecNo = 3;
            tmpmodel.Id = id;
            tmpmodel.PrintClss = TrnJuchud.PRINT_CLSS_NO;
            tmpmodel.TraderRegNo1 = TRADER_REG_NO13.Text;
            tmpmodel.TraderRegNo2 = TRADER_REG_NO23.Text;
            tmpmodel.TraderRegNo = TRADER_REG_NO13.Text + TRADER_REG_NO23.Text;
            sq = TRN_SEAL_QTY3.Text;
            if (StringUtil.isEmpty(sq))
            {
                tmpmodel.SealQty = -1;
            }
            else if (sq.IndexOf("-") != -1)
            {
                //マイナスが入力された場合には
                tmpmodel.SealQty = -2;
            }
            else if (!StringUtil.isHankakuNumber(sq))
            {
                tmpmodel.SealQty = -2;
            }
            else
            {
                tmpmodel.SealQty = int.Parse(TRN_SEAL_QTY3.Text);
            }
            if (tmpmodel.SealQty == -1)
            {
                model[2] = null;
                LogManager.info("seal_seal_order_represen_detail getJuchuDFromParameter null! : 3");
            }
            else
            {
                LogManager.info("seal_seal_order_represen_detail getJuchuDFromParameter set!  : 3");
                model[2] = tmpmodel;
            }


            //モデルに値をセットする。
            tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo = "";
            id.JuchuRecNo = 4;
            tmpmodel.Id = id;
            tmpmodel.PrintClss = TrnJuchud.PRINT_CLSS_NO;
            tmpmodel.TraderRegNo1 = TRADER_REG_NO14.Text;
            tmpmodel.TraderRegNo2 = TRADER_REG_NO24.Text;
            tmpmodel.TraderRegNo = TRADER_REG_NO14.Text + TRADER_REG_NO24.Text;
            sq = TRN_SEAL_QTY4.Text;
            if (StringUtil.isEmpty(sq))
            {
                tmpmodel.SealQty = -1;
            }
            else if (sq.IndexOf("-") != -1)
            {
                //マイナスが入力された場合には
                tmpmodel.SealQty = -2;
            }
            else if (!StringUtil.isHankakuNumber(sq))
            {
                tmpmodel.SealQty = -2;
            }
            else
            {
                tmpmodel.SealQty = int.Parse(TRN_SEAL_QTY4.Text);
            }
            if (tmpmodel.SealQty == -1)
            {
                model[3] = null;
                LogManager.info("seal_seal_order_represen_detail getJuchuDFromParameter null! : 4");
            }
            else
            {
                LogManager.info("seal_seal_order_represen_detail getJuchuDFromParameter set!  : 4");
                model[3] = tmpmodel;
            }

            //モデルに値をセットする。
            tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo = "";
            id.JuchuRecNo = 5;
            tmpmodel.Id = id;
            tmpmodel.PrintClss = TrnJuchud.PRINT_CLSS_NO;
            tmpmodel.TraderRegNo1 = TRADER_REG_NO15.Text;
            tmpmodel.TraderRegNo2 = TRADER_REG_NO25.Text;
            tmpmodel.TraderRegNo = TRADER_REG_NO15.Text + TRADER_REG_NO25.Text;
            sq = TRN_SEAL_QTY5.Text;
            if (StringUtil.isEmpty(sq))
            {
                tmpmodel.SealQty = -1;
            }
            else if (sq.IndexOf("-") != -1)
            {
                //マイナスが入力された場合には
                tmpmodel.SealQty = -2;
            }
            else if (!StringUtil.isHankakuNumber(sq))
            {
                tmpmodel.SealQty = -2;
            }
            else
            {
                tmpmodel.SealQty = int.Parse(TRN_SEAL_QTY5.Text);
            }
            if (tmpmodel.SealQty == -1)
            {
                model[4] = null;
                LogManager.info("seal_seal_order_represen_detail getJuchuDFromParameter null! : 5");
            }
            else
            {
                LogManager.info("seal_seal_order_represen_detail getJuchuDFromParameter set!  : 5");
                model[4] = tmpmodel;
            }

            order.setJuchu_D(model);
            order.setJuchu_H(tjh);

            //検証
            LoginModel loginModel = (LoginModel)Session["loginModel"];
            TRN_JuchuManager validation = new TRN_JuchuManager();
            List<string> errorCodeList = validation.checkInsertOrder_Represen(order, Resources.Resource.ResourceManager);
            msg.Text = ErrorCodeUtil.errorCodeListToHtml(errorCodeList);

            if (errorCodeList.Count == 0)
            {
                Session["order"] = order;
                Response.Redirect("seal_order_represen_conf.aspx");
            }


        }
        catch (Exception ex)
        {
            LogManager.error("seal_seal_order " + ex.Message +ex.StackTrace);


        }


    }
    
}
